$(function(){
		var srcImg=serverIP+"/code?rnd=" + Math.random();
	  $("#codeImg").attr("src", srcImg);
	  $("#username").focus(function(){
	  		$("#username").next("i").css('color','#557799');
	  }).blur(function(){
	  		$("#username").next("i").css('color','#909090');
	  })
	  $("#password").focus(function(){
	  		$("#password").next("i").css('color','#557799');
	  }).blur(function(){
	  		$("#password").next("i").css('color','#909090');
	  })
});
function changeCode(){
	var srcImg=serverIP+"/code?rnd=" + Math.random();
	$("#codeImg").attr("src",srcImg);
}
$("#loginIn").click(function() {
	var username = $("#username").val();
	var password = $("#password").val();
	var captcha = $("#captcha").val();
		if(username != "") {
			if(password != "") {
				if(captcha != "") {
					$.ajax({
						type:"post",
						url:serverIP+"/user/login/check",
						dataType:"json",
						xhrFields: {
			                withCredentials: true    // 设置sessionId相同
			            },
			            async: false,
						data:{
							"username":username,
							"password":password,
							"picCode":captcha
						},	
						success:function(data){
							if(data.code==0){
								getType();
							}else{
								if(data.msg=="errorPassword"){
										$("#tips").html("密码错误");
										$("#password").val(""); 
										$("#captcha").val("");
										$("#password").focus();
										changeCode();
								}
								if(data.msg=="errorPicCode"){
										$("#tips").html("验证码错误");
										$("#captcha").val(""); 
										$("#captcha").focus();
										changeCode();
								}
								if(data.msg=="noRecord"){
										$("#tips").html("用户名错误");
										$("#username").val(""); 
										$("#username").focus();
										changeCode();
								}
							}
						},
						error:function(e){
//							alert("error")
						}
					});
			} else {
				$("#tips").html("请输入验证码");
			}
		} else {
			$("#tips").html("请输入密码");
		}
	} else {
		$("#tips").html("请输入账号");
	}
});
function getType(){
	$.ajax({  
        type : "get",  
        url : serverIP+'/user/getAdminInfo',  
        xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        async: false,
        success : function(data){ 
        	if(data.code==0){
        		if(data.data.type =="11"){
        			window.location.href="index.html";
        		}
        		if(data.data.type =="01"){
        			alert("admin禁止本网址登录!");
        		}
        		if(data.data.type =="31"){
        			window.location.href="errordetail.html";
        		}
        	}else{
        		alert(data.msg);
        	}
        }  
    });
}

//键盘Enter登录
$("body").keydown(function(event) {
     if (event.keyCode == "13") {//keyCode=13是回车键
         $('#loginIn').click();
     }
 });